package com.sys.web.async;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.constant.UploadAndDownloadPath;
import com.sys.dao.FileDao;
import com.sys.dao.UploadDao;
import com.sys.dao.impl.FileDaoImpl;
import com.sys.dao.impl.UploadDaoImpl;
import com.sys.dao.impl.UserDaoImpl;
import com.sys.domain.File;
import com.sys.service.impl.UploadOperationImpl;

import net.sf.json.JSONObject;

@WebServlet("/adminAsy")
public class AdminAsy extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		System.out.println(method);
		String user = (String)request.getSession().getAttribute("user");
		if(user == null||!user.equals("admin")) {
			response.sendRedirect(request.getContextPath()+java.io.File.separator+"homeUI");
			return;
		}
		if(method.equals("approve")) {
			try {
				approve(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (method.equals("notApprove")) {
			try {
				notApprove(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (method.equals("deleteFile")) {
			try {
				deleteFile(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (method.equals("frozenFile")) {
			try {
				frozenFile(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (method.equals("userFrozen")) {
			try {
				userFrozen(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (method.equals("postNotice")) {
			postNotice(request, response);
		}else if (method.equals("changeFileType")) {
			try {
				changeFileType(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void changeFileType(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, IOException {
		String fileId = request.getParameter("fileId");
		String fileType = request.getParameter("fileType");
		System.out.println(fileId + fileType);
		Boolean b = new FileDaoImpl().changeFileType(fileId, fileType);
		Map<String, Boolean> map = new HashMap<>();
 		map.put("isSuc", b);
 		response.getWriter().write(JSONObject.fromObject(map).toString());
	}

	private void postNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
		java.io.File notice = new java.io.File("E:\\Java2018\\workspace\\Sharing-Your-Story\\src\\notice");
		FileOutputStream out = new FileOutputStream(notice);
        BufferedOutputStream bufOut = new BufferedOutputStream(out);
        bufOut.write(request.getParameter("msg").getBytes());
        bufOut.flush();
        bufOut.close();
        out.close();
        Map<String, Boolean> map = new HashMap<>();
 		map.put("isSuc", true);
 		response.getWriter().write(JSONObject.fromObject(map).toString());
	}

	private void userFrozen(HttpServletRequest request, HttpServletResponse response) 
			throws SQLException, IOException {
		String userId = request.getParameter("userId");
		String status = request.getParameter("status");
		Boolean b1 = new UserDaoImpl().changeStatus(userId, status);
		Map<String, Boolean> map = new HashMap<>();
 		map.put("isSuc", b1);
 		response.getWriter().write(JSONObject.fromObject(map).toString());
	}

	private void frozenFile(HttpServletRequest request, HttpServletResponse response) 
			throws SQLException, IOException {
		String fileId = request.getParameter("fileId");
		String upId = request.getParameter("upId");
		String status = request.getParameter("status");
		FileDao fileDao = new FileDaoImpl();
		UploadDao uDao = new UploadDaoImpl();
		Boolean b1 = fileDao.changeStatus(upId+fileId, status);
		Boolean b2 = uDao.changeStatus(upId+fileId, status);
		Map<String, Boolean> map = new HashMap<>();
 		map.put("isSuc", b1&b2);
 		response.getWriter().write(JSONObject.fromObject(map).toString());
	}

	private void deleteFile(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		String fileId = request.getParameter("fileId");
		String upId = request.getParameter("upId");
		FileDao fileDao = new FileDaoImpl();
		Boolean b1 = fileDao.deleteFile(upId+fileId);
		Boolean b2 = new java.io.File(UploadAndDownloadPath.path+java.io.File.separator+upId+fileId).delete();
		Map<String, Boolean> map = new HashMap<>();
 		map.put("isSuc", b1&b2);
 		response.getWriter().write(JSONObject.fromObject(map).toString());
	}

	private void notApprove(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		String fileId = request.getParameter("fileId");
		String upId = request.getParameter("upId");
		UploadDao uDao = new UploadDaoImpl();
		Boolean b1 = uDao.changeStatus(upId+fileId, "3");
		Boolean b2 = new java.io.File(UploadAndDownloadPath.path+"\\"+upId+fileId).delete();
		Map<String, Boolean> map = new HashMap<>();
 		map.put("isSuc", b1&b2);
 		response.getWriter().write(JSONObject.fromObject(map).toString());
	}

	private void approve(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, IOException {
		String fileId = request.getParameter("fileId");
		String upId = request.getParameter("upId");
		String fileType = request.getParameter("fileType");
		UploadDao uDao = new UploadDaoImpl();
		Boolean b1 = uDao.changeStatus(upId+fileId, "1");
		FileDao fDao = new FileDaoImpl();
		File file = new UploadOperationImpl().setFileApproved(upId+fileId, fileType);
 		Boolean b2 = fDao.insertRecord(file);
 		System.out.println(b1&b2);
 		Map<String, Boolean> map = new HashMap<>();
 		map.put("isSuc", b1&b2);
 		response.getWriter().write(JSONObject.fromObject(map).toString());
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}